home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Icon 8.1 / mep1 / Samples / Programs / deal.icn < prev    next >
Encoding:
Text File  |  1989-05-09  |  2.5 KB  |  109 lines  |  [TEXT/PICN]

  1. ############################################################################
  2. #
  3. #  deal.icn
  4. #  
  5. #     This program shuffles, deals, and displays hands in the game
  6. #  of bridge.  An example of the output of deal is
  7. #       ---------------------------------
  8. #  
  9. #                 S: KQ987
  10. #                 H: 52
  11. #                 D: T94
  12. #                 C: T82
  13. #  
  14. #       S: 3                S: JT4
  15. #       H: T7               H: J9863
  16. #       D: AKQ762           D: J85
  17. #       C: QJ94             C: K7
  18. #  
  19. #                 S: A652
  20. #                 H: AKQ4
  21. #                 D: 3
  22. #                 C: A653
  23. #  
  24. #       ---------------------------------
  25. #  
  26. #  Options: The following parameter string options are available:
  27. #  
  28. #       -h n Produce n hands. The default is 4.
  29. #  
  30. #       -s n Set the seed for random generation to n.  Different
  31. #            seeds give different hands.  The default seed is 0.
  32. #  
  33. ############################################################################
  34. #
  35. # Links: getopt, shuffle
  36. #
  37. ############################################################################
  38.  
  39. link getopt, shuffle
  40.  
  41. global deck, deckimage, handsize, suitsize, denom, rank, blanker
  42.  
  43. procedure main(args)
  44.     local hands, opts
  45.  
  46.     deck := deckimage := &lcase || &ucase    # initialize global variables
  47.     handsize := suitsize := *deck / 4
  48.     rank := "AKQJT98765432"
  49.     blanker := repl(" ",suitsize)
  50.     denom := &lcase[1+:suitsize]
  51.  
  52.     opts := getopt(args,"h+s+")[1]
  53.     hands := \opts["h"] | 4
  54.     &random := \opts["s"]
  55.  
  56.     every 1 to hands do
  57.         display()
  58.  
  59. end
  60.  
  61. #  Display the hands
  62. #
  63. procedure display()
  64.     local layout, i
  65.     static bar, offset
  66.  
  67.     initial {
  68.         bar := "\n" || repl("-",33)
  69.         offset := repl(" ",10)
  70.         }
  71.  
  72.     deck := shuffle(deck)
  73.     layout := []
  74.     every push(layout,show(deck[(0 to 3) * handsize + 1 +: handsize]))
  75.  
  76.     write()
  77.     every write(offset,!layout[1])
  78.     write()
  79.     every i := 1 to 4 do
  80.         write(left(layout[4][i],20),layout[2][i])
  81.     write()
  82.     every write(offset,!layout[3])
  83.     write(bar)
  84. end
  85.  
  86. #  Put the hands in a form to display
  87. #
  88. procedure show(hand)
  89.     static clubmap, diamondmap, heartmap, spademap
  90.     initial {
  91.         clubmap := denom || repl(blanker,3)
  92.         diamondmap := blanker || denom || repl(blanker,2)
  93.         heartmap := repl(blanker,2) || denom || blanker
  94.         spademap := repl(blanker,3) || denom
  95.         }
  96.     return [
  97.         "S: " || arrange(hand,spademap),
  98.         "H: " || arrange(hand,heartmap),
  99.         "D: " || arrange(hand,diamondmap),
  100.         "C: " || arrange(hand,clubmap)
  101.         ]
  102. end
  103.  
  104. #  Arrange hands for presentation
  105. #
  106. procedure arrange(hand,suit)
  107.     return map(map(hand,deckimage,suit) -- ' ',denom,rank)
  108. end
  109.